package com.googlecode.stockhamster.rumor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class IncRatio {
	private String companyName;
	private String category;
	public double ratio1;
	public double ratio2;
	public double ratio3;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public IncRatio(String companyName, String category, double r1, double r2,
			double r3) {
		this.companyName = companyName;
		this.category = category;
		this.ratio1 = r1;
		this.ratio2 = r2;
		this.ratio3 = r3;
	}

	public IncRatio() {
	}

	// for database , (companyName + category) = main keyword

	public static IncRatio get(String companyName, String category) {
		// get the IncRatio item where companyName = . and category = .
		// return null if not exist
		IncRatio tempAccount = null;
		try {
			String sqlGetAccount;
			if (companyName != null) {
				sqlGetAccount = "select * from rumorIncRatio where companyName=?";
				state = ConnectDatabase.getConnection().prepareStatement(
						sqlGetAccount);
				state.setString(1, companyName);
			} else {
				sqlGetAccount = "select * from rumorIncRatio where category=?";
				state = ConnectDatabase.getConnection().prepareStatement(
						sqlGetAccount);
				state.setString(1, category);
			}
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempAccount = new IncRatio(rs.getString(1), rs.getString(2), rs
					.getDouble(3), rs.getDouble(4), rs.getDouble(5));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tempAccount;
	}

	public boolean insert() {
		// insert this
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into rumorIncRatio values(?,?,?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setString(1, this.companyName);
			state.setString(2, this.category);
			state.setDouble(3, this.ratio1);
			state.setDouble(4, this.ratio2);
			state.setDouble(5, this.ratio3);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				if (state != null) {
					try {
						state.close();
					} catch (SQLException e) {
					}
				}
			}
		}
		return flag;
	}

	public boolean save() {
		// save this
		boolean flag = false;
		try {
			String sqlSetArbiterAccount;
			if (companyName != null) {
				sqlSetArbiterAccount = "update rumorIncRatio set ratio1=?, ratio2=?, ratio3=? where companyName=?";
				state = ConnectDatabase.getConnection().prepareStatement(
						sqlSetArbiterAccount);
				state.setString(4, this.companyName);
				state.setDouble(1, this.ratio1);
				state.setDouble(2, this.ratio2);
				state.setDouble(3, this.ratio3);
			} else {
				sqlSetArbiterAccount = "update rumorIncRatio set ratio1=?, ratio2=?, ratio3=? where category=?";
				state = ConnectDatabase.getConnection().prepareStatement(
						sqlSetArbiterAccount);
				state.setString(4, this.category);
				state.setDouble(1, this.ratio1);
				state.setDouble(2, this.ratio2);
				state.setDouble(3, this.ratio3);
			}
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}
}
